package edu.faytechcc.csc251.gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/**
 *
 * @author bryanc7064
 */
public class SizeCrustPanel extends JPanel {

    private ArrayList<String> crusts;
    private ArrayList<Size> sizes;
    private ArrayList<JRadioButton> sizeButtons;
    private ArrayList<JRadioButton> crustButtons;

    public SizeCrustPanel() {
        readFile();
        
        buildPanel();
        //this.clear();
    }

    private void readFile() {
        try {
            File file = new File("sizes-crusts.csv");
            Scanner inFile = new Scanner(file);
            
            String line;
            
            //instantiate the ArrayList of Sizes
            sizes = new ArrayList<>();
            while(inFile.hasNext())
            {
                line = inFile.nextLine();
                if(!line.startsWith("#")) //ignore comments!
                {
                        if(crusts == null)
                        {
                            //The first line of the file has the crust types. This is the first line because the ArrayList of crusts has NOT yet been created.
                            crusts = new ArrayList<>(); //create it
                            //Add all of the csv crust types from the line to the ArrayList of crusts.
                            crusts.addAll(Arrays.asList(line.split(",")));
                        }
                        else
                        {
                            //get all fo the size options now
                            String[] parts = line.split(","); //split the csv
                            Size size = new Size(); //create a Size object to hold the size info
                            size.name = parts[0]; //add the name
                            for(int index = 1; index < parts.length; index++) //if there are disallowed crust types, add them to the Size object.
                            {
                                size.disAllowedCrusts.add(parts[index]);
                            }
                            sizes.add(size); //add this new size to the ArrayList
                        }

                }
               
            }
        } catch (Exception e) {
            //hey, if the file fails to load, crash!
            JOptionPane.showMessageDialog(this, "Failed to open sizes-crusts.csv");
            System.exit(0);
        }
    }

    private void buildPanel() {
        this.setLayout(new BorderLayout());
        JPanel container = new JPanel(new BorderLayout());
        JPanel sizePanel = buildSizePanel();
        JPanel crustPanel = buildCrustPanel();
        
        container.add(sizePanel, BorderLayout.NORTH);
        container.add(crustPanel, BorderLayout.SOUTH);
        this.add(container, BorderLayout.NORTH);
    }

    /**
     * Set this Panel back to its initial state.
     */
    public void clear()
    {
        getSizeButton("Small").setSelected(true);
        getCrustButton("Hand-Tossed").setSelected(true);
        disableCrusts("Thin");
    }
    
    private void disableCrusts(String size)
    {
        for(Size s : sizes)
        {
            if(s.name.equals(size))
            {
                for(String crustName : s.disAllowedCrusts)
                {
                    getCrustButton(crustName).setEnabled(false);
                }
            }
        }
    }
    
    private JRadioButton getCrustButton(String text)
    {
        for(JRadioButton butt : crustButtons)
        {
            if(butt.getText().equals(text))
            {
                return butt;
            }
        }
        return null;
    }
    
    private JRadioButton getSizeButton(String text)
    {
        for(JRadioButton butt : sizeButtons)
        {
            if(butt.getText().equals(text))
            {
                return butt;
            }
        }
        return null;
    }
    
    private JPanel buildSizePanel() {
        JPanel sizePanel = new JPanel(new GridLayout(sizes.size(), 1));
        sizePanel.setBorder(BorderFactory.createTitledBorder("Size"));
        ButtonGroup sizeGroup = new ButtonGroup();
        
        sizeButtons = new ArrayList<>();
        for(Size size : sizes)
        {
            JRadioButton temp = new JRadioButton(size.name);
            sizeGroup.add(temp);
            //need to remember to add the ActionListener later...
            //temp.addActionListener(new SomeLIstener());
            sizePanel.add(temp);
        }
        //sizeButtons.get(0).setSelected(true); //not use this...
        return sizePanel;
    }

    private JPanel buildCrustPanel() {
        JPanel crustPanel = new JPanel(new GridLayout(crusts.size(), 1));
        crustPanel.setBorder(BorderFactory.createTitledBorder("Crust"));
        ButtonGroup crustGroup = new ButtonGroup();
        
        sizeButtons = new ArrayList<>();
        for(String name : crusts)
        {
            JRadioButton temp = new JRadioButton(name);
            crustGroup.add(temp);
            //need to remember to add the ActionListener later...
            //temp.addActionListener(new SomeLIstener());
            crustPanel.add(temp);
        }
        //sizeButtons.get(0).setSelected(true); //not use this...

        return crustPanel;
    }
    
    /**
     * Inner class used to hold the information about each size.
     */
    class Size {

        String name; 
        ArrayList<String> disAllowedCrusts = new ArrayList<>();
    }
}
